package com.foreknow.service.impl;


import com.foreknow.dao.impl.UserDaoImpl;
import com.foreknow.bean.User;
import com.foreknow.service.UserService;
import com.foreknow.util.DbHelper;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {

    UserDaoImpl userDao=new UserDaoImpl();
    DataSource dataSource = DbHelper.getDataSource();

    @Override
    public boolean addUser(Object[] params) {
        //不要在service层写sql
        String sql="insert into user(username,account,password,phone,email,avatr) values(?,?,?,?,?,?)";
        Connection conn=null;
        int insert=0;
        try {
            insert = userDao.insert(sql, params);
            //事物操作
            conn=dataSource.getConnection();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return insert == 1;
    }

    @Override
    public List<User> login(Object... params) {
        String sql="select * from user where username=? and password=?";
        List<User> users=null;
        try {
            users = userDao.queryByCondition(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }

//    @Override
//    public User getUserById(Integer id) {
//        String sql="select * from user where id=?";
//        try {
//            userDao.queryByCondition(sql,id);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }


}
